package cn.tedu.egweibo06.controller;

import cn.tedu.egweibo06.common.response.JsonResult;
import cn.tedu.egweibo06.mapper.CommentMapper;
import cn.tedu.egweibo06.pojo.dto.CommentDTO;
import cn.tedu.egweibo06.pojo.entity.Comment;
import cn.tedu.egweibo06.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Validated
@Api(tags = "03.评论模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation("发布评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        log.debug("commentDTO" + commentDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    @ApiOperation("获取评论列表")
    @PostMapping("selectByWeiboId")
    public JsonResult selectByWeiboId(@Range(min = 200,max = 299,message = "微博id范围在200-299之间") @RequestParam int id) {
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}
